using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using ns20;

namespace Newtonsoft.Json.Converters
{
	public class ExpandoObjectConverter : JsonConverter
	{
		public override bool CanWrite => false;

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			return method_0(reader);
		}

		private object method_0(JsonReader jsonReader_0)
		{
			do
			{
				if (jsonReader_0.JsonToken_0 == JsonToken.Comment)
				{
					continue;
				}
				switch (jsonReader_0.JsonToken_0)
				{
				default:
					if (JsonReader.smethod_0(jsonReader_0.JsonToken_0))
					{
						return jsonReader_0.Object_0;
					}
					throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected token when converting ExpandoObject: {0}".smethod_0(CultureInfo.InvariantCulture, jsonReader_0.JsonToken_0));
				case JsonToken.StartObject:
					return method_2(jsonReader_0);
				case JsonToken.StartArray:
					return method_1(jsonReader_0);
				}
			}
			while (jsonReader_0.Read());
			throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading ExpandoObject.");
		}

		private object method_1(JsonReader jsonReader_0)
		{
			IList<object> list = new List<object>();
			while (jsonReader_0.Read())
			{
				switch (jsonReader_0.JsonToken_0)
				{
				case JsonToken.Comment:
					continue;
				case JsonToken.EndArray:
					return list;
				}
				object item = method_0(jsonReader_0);
				list.Add(item);
			}
			throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading ExpandoObject.");
		}

		private object method_2(JsonReader jsonReader_0)
		{
			IDictionary<string, object> dictionary = new ExpandoObject();
			while (jsonReader_0.Read())
			{
				switch (jsonReader_0.JsonToken_0)
				{
				case JsonToken.PropertyName:
				{
					string key = jsonReader_0.Object_0.ToString();
					if (!jsonReader_0.Read())
					{
						throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading ExpandoObject.");
					}
					object obj2 = (dictionary[key] = method_0(jsonReader_0));
					break;
				}
				case JsonToken.EndObject:
					return dictionary;
				}
			}
			throw JsonSerializationException.smethod_1(jsonReader_0, "Unexpected end when reading ExpandoObject.");
		}

		public override bool CanConvert(Type objectType)
		{
			return objectType == typeof(ExpandoObject);
		}
	}
}
